package com.wangzhen.spring.bean.definition;

import com.wangzhen.spring.ioc.dependcy.entity.User;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.GenericBeanDefinition;

/**
 * BeanDefinition
 * @author dell
 * @since 2020/10/21 12:54
 */
public class BeanDefinitionCreateDemo {

    public static void main(String[] args) {
        // 1、通过BeandefinitionBuilder构建
        BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(User.class);
        // 设置字段属性
        beanDefinitionBuilder.addPropertyValue("name", "zhangsan");
        beanDefinitionBuilder.addPropertyValue("age", 20);
        // 获取到的bean并非beanDefinition的最终结果，还可以通过set方法进行其他属性设置
        BeanDefinition beanDefinition = beanDefinitionBuilder.getBeanDefinition();
        // beanDefinition.setLazyInit();
        // beanDefinition.setScope();

        // 2、通过AbstractBeanDefinition的子类或派生类
        GenericBeanDefinition genericBeanDefinition = new GenericBeanDefinition();
        genericBeanDefinition.setBeanClass(User.class);
        MutablePropertyValues mutablePropertyValues = new MutablePropertyValues();
        // mutablePropertyValues.addPropertyValue("name", "zhangsan");
        // mutablePropertyValues.addPropertyValue("age",20);
        mutablePropertyValues.add("name", "zhangsan").add("age", 10);

        genericBeanDefinition.setPropertyValues(mutablePropertyValues);
    }
}
